package com.zjt.service.product;

import com.zjt.dao.brandcategpry.IBrandCategoryDao;
import com.zjt.dao.brandcategpry.IBrandDao;
import com.zjt.entity.brandcategory.Brand;
import com.zjt.entity.brandcategory.BrandCategory;
import com.zjt.utils.DateUtil;
import com.zjt.web.api.vo.base.PageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * User: xueyanbeijing
 * Date: 12-8-29
 * Time: AM9:38
 * 品牌（商标）类型
 * To change this template use File | Settings | File Templates.
 */
@Component
public class BrandCategoryService implements IBrandCategoryService{
    private static Logger logger = LoggerFactory.getLogger(CodeService.class);
    private IBrandDao brandDao;
    private IBrandCategoryDao brandCategoryDao;
    @Autowired
    public void setBrandDao(IBrandDao brandDao) {
        this.brandDao = brandDao;
    }
    @Autowired
    public void setBrandCategoryDao(IBrandCategoryDao brandCategoryDao) {
        this.brandCategoryDao = brandCategoryDao;
    }

    @Override
    public BrandCategory saveBrandCategory(long brandId, long categoryId) {
        BrandCategory brandCategory = null;
        try
        {
            brandCategory = this.brandCategoryDao.findByBrandIdAndCategoryId(brandId, categoryId);
            if (brandCategory==null)
            {
                brandCategory.setEnableFlag("Y");
                brandCategory.setBrandId(brandId);
                brandCategory.setCategoryId(categoryId);
                brandCategory.setCreateDate(DateUtil.getCurrentDate());
                brandCategory.setUpdateDate(DateUtil.getCurrentDate());
                logger.info("写入数据前brandCategory="+brandCategory.toString());
                this.brandCategoryDao.save(brandCategory);
            }
        }catch (Exception e4){
            //TODO
        }
        return brandCategory;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean removeBranCategory(long brandId, long categoryId) {
        boolean doFlag = false;
        //TODO 去掉商标与大类的关联，不能将数据删除 ，仅将enable_flag置为N
        try
        {
            BrandCategory brandCategory = this.brandCategoryDao.findByBrandIdAndCategoryId(brandId, categoryId);
            if (brandCategory!=null)
            {
                brandCategory.setEnableFlag("N");
                this.brandCategoryDao.save(brandCategory);
                doFlag = true;
            }
        }catch (Exception e4){
            doFlag = false;
        }
        return doFlag;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean saveBrand(String chName, String enName, String alias, long factoryId) {
        //TODO 入品牌库（商标）商标中文名称不能相同 ,如果相同，将覆盖
        boolean doFlag = false;
        try
        {
            Brand brand = this.brandDao.findByChName(chName.trim()).get(0);
            if (brand!=null)
            {
                Brand brand1 = new Brand();
                brand1.setCategoryCount(0);
                brand1.setAlias(alias);
                brand1.setChName(brand.getChName());
                brand1.setEnName(brand.getEnName());
                brand1.setCreateDate(brand.getCreateDate());
                brand1.setUpdateDate(DateUtil.getCurrentDate());
                brand1.setEnableFlag("Y");
                this.brandDao.save(brand1);
                doFlag = true;
            }else
            {
                Brand brand2 = new Brand();
                brand2.setCategoryCount(0);
                brand2.setAlias(chName);
                brand2.setChName(chName);
                brand2.setEnName(enName);
                brand2.setCreateDate(DateUtil.getCurrentDate());
                brand2.setUpdateDate(DateUtil.getCurrentDate());
                brand2.setEnableFlag("Y");
                this.brandDao.save(brand2);
                doFlag = true;
            }
        }catch (Exception e4){
            doFlag = false;
        }
        return doFlag;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Brand getBrandById(long brandId) {
        return this.brandDao.findOne(brandId);
     //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Page<BrandCategory> getBrandCategoryList(PageVO pageVO) {
        Sort.Direction direction =  Sort.Direction.DESC;
        PageRequest pageRequest = new PageRequest(pageVO.getPageNo(),pageVO.getPageSize(),direction,"id");
        Page<BrandCategory> list = this.brandCategoryDao.findAll(pageRequest);
        return list;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
